package util

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"mime/multipart"
	"os"
)

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err != nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, nil
}

func GetMd5ByFile(file multipart.File) string {
	md5 := md5.New()
	io.Copy(md5, file)
	return hex.EncodeToString(md5.Sum(nil))
}

func GetMd5ByPath(path string) string {
	file, err := os.Open(path)
	if err != nil {
		fmt.Println(err)
	}
	return GetMd5ByFile(file)
}
